//
//  simplest_eagl_rgb_render_shader.h
//  QH_Simplest_EAGL_Demo
//
//  Created by Anakin chen on 2021/2/17.
//

#ifndef simplest_eagl_rgb_render_shader_h
#define simplest_eagl_rgb_render_shader_h

#define STRINGIZE(x) #x
#define STRINGIZE2(x) STRINGIZE(x)
#define SHADER_STRING(text) @ STRINGIZE2(text)

NSString *const vvertexShaderString = SHADER_STRING
(
 attribute vec4 position;
 attribute vec2 texcoord;
 varying vec2 v_texcoord;
 
 void main()
 {
     gl_Position = position;
     v_texcoord = texcoord.xy;
 }
);

NSString *const rgbFragmentShaderString = SHADER_STRING
(
 varying highp vec2 v_texcoord;
 uniform sampler2D inputImageTexture;
 
 mediump vec3 rgb;
 
 void main()
 {
    rgb.r = texture2D(inputImageTexture, v_texcoord).r;
    rgb.g = texture2D(inputImageTexture, v_texcoord).g;
    rgb.b = texture2D(inputImageTexture, v_texcoord).b;
     gl_FragColor = vec4(rgb, 1);
 }
);

NSString *const rgbFragmentShaderString2 = SHADER_STRING
(
 varying highp vec2 v_texcoord;
 uniform sampler2D inputImageTexture;
 
 uniform mediump float v_mesh[8];
 
 void main()
 {
    mediump float fx = v_texcoord.x;
    mediump float fy = v_texcoord.y;
    
    mediump float x[4];
    mediump float y[4];
    
    x[0] = v_mesh[0];
    x[1] = v_mesh[2];
    x[3] = v_mesh[4];
    x[2] = v_mesh[6];
    
    y[0] = v_mesh[1];
    y[1] = v_mesh[3];
    y[3] = v_mesh[5];
    y[2] = v_mesh[7];
    
    mediump float a;
    mediump float b;
    mediump float c;
    mediump float d;//分别存四个向量的计算结果;
    a = (x[1] - x[0])*(fy - y[0]) - (y[1] - y[0])*(fx - x[0]);
    b = (x[2] - x[1])*(fy - y[1]) - (y[2] - y[1])*(fx - x[1]);
    c = (x[3] - x[2])*(fy - y[2]) - (y[3] - y[2])*(fx - x[2]);
    d = (x[0] - x[3])*(fy - y[3]) - (y[0] - y[3])*(fx - x[3]);
    if((a >= 0.0 && b >= 0.0 && c >= 0.0 && d >= 0.0) || (a <= 0.0 && b <= 0.0 && c <= 0.0 && d <= 0.0)) {
        gl_FragColor = vec4(texture2D(inputImageTexture, v_texcoord).rgb, 1);
    }
    else {
        gl_FragColor = vec4(1, 1, 1, 0);
    }
 }
);

#endif /* simplest_eagl_rgb_render_shader_h */
